// componnets/optionBoard/optionBoard.js
import tool from '../../modules/tool'
import store from '../../modules/store'
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    data: {
      type: Object,
      default: {}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    pd_price: 0,
    pd_priceSingle: 0,
    pd_selectedOptions: {},
    pd_count: 1,
    pd_type: 1,
    isPro: true,
    form: {}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleInputForm(e) {
      this.setData({
        form: e.detail,
        pd_selectedOptions: e.detail
      })
    },
    handlePayNow() {

      if (!this.data.isPro) {
        try {
          tool.handleCheckForm(this.data.form, this.properties.data.options)
        } catch (error) {
          tool.handleShowToast(`${error}`, 'error')
          return
        }
      } else {
        let options = this.data.data.options
        let selectedOptions = this.data.pd_selectedOptions
        for (let i = 0; i < options.length; i++) {
          if (options[i].required) {
            if (selectedOptions[options[i].title] === undefined) {
              tool.handleShowToast(`请选择${options[i].title}`)
              return
            }
          }
        }
      }

      let t = this.properties.data
      t.num = this.data.pd_count
      t.itemId = t.id
      t.isPro = this.data.isPro
      t.priceWithFloatDisplay = tool.formatPrice(t.price)
      t.priceWithFloatDisplayTotal = tool.formatPrice(t.price * t.num)
      // delete t.id
      t.options = this.data.pd_selectedOptions
      tool.handleShowPayPage([t])
    },
    async handleAddToCart() {

      if (!this.data.isPro) {
        try {
          tool.handleCheckForm(this.data.form, this.properties.data.options)
        } catch (error) {
          console.log(error)
          tool.handleShowToast(`${error}`, 'error')
          return
        }
      } else {
        let options = this.data.data.options
        let selectedOptions = this.data.pd_selectedOptions
        for (let i = 0; i < options.length; i++) {
          if (options[i].required) {
            if (selectedOptions[options[i].title] === undefined) {
              tool.handleShowToast(`请选择${options[i].title}`)
              return
            }
          }
        }
      }
      let re = await tool.handleAddToCart(this.data.isPro ? 1 : 0, this.data.data.id, this.data.pd_count, this.data.pd_priceSingle, this.data.pd_selectedOptions)
      if (re) {
        await tool.handleUpdateCartNum()
        tool.handleShowToast('添加成功', 'success')
        store.setItem('indexHidePageBoard')
      }
    },
    handleInputUserName(e) {
      e = e.detail.value
      let t = this.data.pd_selectedOptions
      t.name = e
      this.setData({
        pd_selectedOptions: t
      })
    },
    handleInputWeChat(e) {
      e = e.detail.value
      let t = this.data.pd_selectedOptions
      t.wechat = e
      this.setData({
        pd_selectedOptions: t
      })
    },
    handleChangeCount(e) {
      e = e.currentTarget.dataset
      let t = this.data.pd_count
      if (e.up) {
        t++
      } else {
        t--
      }
      if (t < 1) t = 1
      this.setData({
        pd_count: t
      })
      this.pd_updatePrice()
    },
    handleOptionClick(e) {
      e = e.currentTarget.dataset
      if (e.selected) {
        let t = this.data.pd_selectedOptions
        delete t[e.item]
        this.setData({
          pd_selectedOptions: t
        })
        this.pd_updatePrice()
        return
      }
      let t = this.data.pd_selectedOptions
      t[e.item] = e.subitem
      this.setData({
        pd_selectedOptions: t
      })
      this.pd_updatePrice()
    },
    pd_updatePrice() {
      let data = this.data.pd_selectedOptions
      let p = this.data.data.price
      for (let i = 0; i < Object.keys(data).length; i++) {
        let value = data[Object.keys(data)[i]]
        let key = Object.keys(data)[i]
        if (!this.data.data.options) continue
        for (let i1 = 0; i1 < this.data.data.options.length; i1++) {
          if (this.data.data.options[i1].title == key) {
            let ind = this.data.data.options[i1].items.indexOf(value)
            if (ind + 1) {
              p += this.data.data.options[i1].prices[ind]
            }
          }
        }
      }
      let item = {
        price: p * this.data.pd_count
      }
      this.setData({
        pd_priceSingle: p,
        pd_price: (item.price / 100).toString() + ((item.price / 100).toString().split('.')[1] ? ((item.price / 100).toString().split('.')[1].length == 1 ? '0' : '') : '')
      })
    },
  },

  attached() {
    this.setData({
      pd_type: this.properties.data.isPro,
      isPro: this.properties.data.isPro,
    })
    this.pd_updatePrice()
  }
})